package com.rdc.qingkou.ui;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.githang.statusbar.StatusBarCompat;
import com.rdc.qingkou.Application.MyApplication;
import com.rdc.qingkou.R;
import com.rdc.qingkou.base.BaseActivity;
import com.rdc.qingkou.contract.RegisterContract;
import com.rdc.qingkou.presenter.RegisterPresenter;

import java.util.Timer;
import java.util.TimerTask;

public class RegisterActivity extends BaseActivity<RegisterPresenter> implements RegisterContract.View {
    private ImageButton mBack;
    private EditText mAccount;
    private EditText mPassword;
    private EditText mPasswordAgain;
    private EditText mVerificationCode;
    private TextView mGetCode;
    private Button mConfirm;
    private int mTime = 60;
    private Timer mTimer ;
    private TimerTask task;
    private boolean mIsEnable;
    private boolean mIsChangePassword;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.windowBackground), true);

        initView();
    }

    private void initView() {
        mBack = (ImageButton) findViewById(R.id.ibtn_back);
        mAccount = (EditText) findViewById(R.id.et_account);
        mPassword = (EditText) findViewById(R.id.et_password);
        mPasswordAgain = (EditText) findViewById(R.id.et_passwordAgain);
        mVerificationCode = (EditText) findViewById(R.id.et_verificationCode);
        mGetCode = (TextView) findViewById(R.id.tv_getCode);
        mConfirm = (Button) findViewById(R.id.btn_confirm);
        Intent intent = getIntent();
       mIsChangePassword =  intent.getBooleanExtra("isChangepassword",false);
        mTimer = new Timer();

        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //获取验证码
        mGetCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.getCode();
            }
        });


        //监听按钮的是否可按
        mVerificationCode.addTextChangedListener(new VerificationCodeTextLitener());


        //检查用户是否注册
        mAccount.addTextChangedListener(new AccountTextListener());
        mIsEnable = false;
        mConfirm.setBackgroundResource(R.drawable.btn_confirmoff_selector);
        mConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             if (mIsEnable){
                 mPresenter.registe(mIsChangePassword);
             }
            }
        });

        MyApplication.addActivity(this);

    }


    @Override
    public String getAccount() {
        return mAccount.getText().toString();
    }

    @Override
    public String getPassword() {
        return mPassword.getText().toString();
    }

    @Override
    public String getPasswordAgain() {
        return mPasswordAgain.getText().toString();
    }

    @Override
    public String getCode() {
        return mVerificationCode.getText().toString();
    }


    @Override
    public RegisterPresenter getInstance() {
        return new RegisterPresenter();
    }


    @Override
    public void jumpToLogin() {
        Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void countdown() {
        task = new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (mTime <= 0) {
                            mGetCode.setEnabled(true);
                            mGetCode.setBackgroundResource(R.drawable.btn_verification_shape);
                            mGetCode.setTextColor(Color.parseColor("#8ab077"));
                            mGetCode.setText("获取验证码");
                            task.cancel();
                        } else {


                            mGetCode.setEnabled(false);
                            mGetCode.setBackgroundResource(R.drawable.btn_verificationoff_shape);
                            mGetCode.setTextColor(Color.parseColor("#dadada"));
                            mGetCode.setText(mTime + "秒后重试");

                        }
                        mTime--;
                    }
                });
            }
        };

        mTime = 60;

        mTimer.schedule(task, 0, 1000);
    }



    @Override
    public void showPasswordInfo() {
        Toast.makeText(RegisterActivity.this, "两次密码输入不同，请重新输入。", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void showPasswordEmptyInfo() {
        Toast.makeText(RegisterActivity.this, "密码不能为空！", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showInputInfo() {
        Toast.makeText(RegisterActivity.this, "请输入正确的手机号码。", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void showRegisted() {
        Toast.makeText(RegisterActivity.this, "该号码已经注册,请直接登录。", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void setButtonClickable(boolean isClickable) {
        mIsEnable = isClickable;
        if (isClickable==true){
            mConfirm.setBackgroundResource(R.drawable.btn_login_selector);
        }else {
            mConfirm.setBackgroundResource(R.drawable.btn_confirmoff_selector);
        }

    }

    @Override
    public void showJumpToLoginInfo(String result) {
        Toast.makeText(RegisterActivity.this, result, Toast.LENGTH_SHORT).show();

    }

    @Override
    public void showRegisteFail(String reason) {
        Toast.makeText(RegisterActivity.this, reason, Toast.LENGTH_SHORT).show();
    }

    class VerificationCodeTextLitener implements TextWatcher {
        /*
    监听输入号码和验证码后，按钮可选
   */
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            mPresenter.setButtonClickable();
        }
    }


    class AccountTextListener implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if(mIsChangePassword==false){
                mPresenter.checkIsRegisted();
            }


        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mTimer != null && task != null) {
            mTimer.cancel();
            task.cancel();
        }
    }


}
